﻿/// <reference path="ListBox.js" />
IM.Control.FriendListItem = function(conf) {
    IM.Control.ListViewItem.apply(this, arguments);

    this.ShowFriend = function() {
        this.Show();
        if (!this.DataItem.Online) {//如果不在线，加入隐身效果样式
            this.Render.addClass("IM_OffLine");
        } else {
            this.Render.removeClass("IM_OffLine");
        }
    };

    //双击好友头像,打开聊天窗口
    this.OnDblClick = function() {
        var win = IM.Chat.ChatWindows.Find(this.DataItem.UserId);
        if (win == null) {
            win = new IM.Control.PersonalChatWindow({ Friend: this.DataItem });
            IM.Chat.ChatWindows.Add(this.DataItem.UserId, win);
        }
        win.Show();
        win.ShowMessage();
        var content;
        if (this.SetContent) {
            content = this.SetContent(this.DataItem);
            this.Render.html(content);
        }
    }

    this.FriendListItemInit = function() {
        this.Render.addClass("IM_Friend");
    };

    this.FriendListItemInit();
};

IM.Control.FriendList = function(conf) {
    IM.Control.ListBox.apply(this, arguments);
    this.DataBind = function() {
        this.Items.Clear();

        for (var i = 0; i < this.DataSource.Count(); i++) {
            this.Items.Add(new IM.Control.FriendListItem({ "DataItem": this.DataSource.Find(i), "SetContent": this.SetItemContent, Container: this.Render }));
        }
    };

    this.SetItemContent = function(data) {
        var newCount = 0;
        if (typeof data.Messages != "undefined")
            for (var i = 0; i < data.Messages.Count(); i++) {
            if (data.Messages.Find(i).IsNew)
                newCount++;
        }
        return "<img src='/Faces/" + data.FacePath + "' class='IM_Face' alt='' /><div class='IM_Name'>" + data.NickName + "(" + newCount + ")</div>";
    };

    this.Show = function() {
        this.Render.empty();
        for (var i = 0; i < this.Items.Count(); i++) {
            this.Items.Find(i).ShowFriend();
        }
    };
};